package cn.iocoder.yudao.module.erp.controller.admin.finance.claim;

import cn.iocoder.yudao.module.erp.controller.admin.finance.claim.vo.ClaimPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.finance.claim.vo.ClaimRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.finance.claim.vo.ClaimSaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.dal.dataobject.finance.claim.ClaimDO;
import cn.iocoder.yudao.module.erp.service.finance.claim.ClaimService;

@Tag(name = "管理后台 - 赔付明细")
@RestController
@RequestMapping("/erp/claim")
@Validated
public class ClaimController {

    @Resource
    private ClaimService claimService;

    @PostMapping("/create")
    @Operation(summary = "创建赔付明细")
    // @PreAuthorize("@ss.hasPermission('erp:claim:create')")
    public CommonResult<Integer> createClaim(@Valid @RequestBody ClaimSaveReqVO createReqVO) {
        return success(claimService.createClaim(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新赔付明细")
    // @PreAuthorize("@ss.hasPermission('erp:claim:update')")
    public CommonResult<Boolean> updateClaim(@Valid @RequestBody ClaimSaveReqVO updateReqVO) {
        claimService.updateClaim(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除赔付明细")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('erp:claim:delete')")
    public CommonResult<Boolean> deleteClaim(@RequestParam("id") Integer id) {
        claimService.deleteClaim(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得赔付明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('erp:claim:query')")
    public CommonResult<ClaimRespVO> getClaim(@RequestParam("id") Integer id) {
        ClaimDO claim = claimService.getClaim(id);
        return success(BeanUtils.toBean(claim, ClaimRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得赔付明细分页")
    // @PreAuthorize("@ss.hasPermission('erp:claim:query')")
    public CommonResult<PageResult<ClaimRespVO>> getClaimPage(@Valid ClaimPageReqVO pageReqVO) {
        PageResult<ClaimDO> pageResult = claimService.getClaimPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClaimRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出赔付明细 Excel")
    // @PreAuthorize("@ss.hasPermission('erp:claim:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClaimExcel(@Valid ClaimPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClaimDO> list = claimService.getClaimPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "赔付明细.xls", "数据", ClaimRespVO.class,
                        BeanUtils.toBean(list, ClaimRespVO.class));
    }

}